package org.jeecg.modules.hc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.mapper.HcSiteAddressDetailsMapper;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description: 站点地址明细
 * @Author: jeecg-boot
 * @Date: 2022-12-13
 * @Version: V1.0
 */
@Service
public class HcSiteAddressDetailsServiceImpl extends ServiceImpl<HcSiteAddressDetailsMapper, HcSiteAddressDetails> implements IHcSiteAddressDetailsService {

    @Resource
    private HcSiteAddressDetailsMapper hcSiteAddressDetailsMapper;

    @Override
    public List<HcSiteAddressDetails> getListByAddressId(String addressId,String merchantId) {
        HcSiteAddressDetails addressDetails = this.getById(addressId);
        String jsonSupplyAddress = addressDetails.getSupplyAddress();
        List<HcSiteAddressDetails> result = new ArrayList<>();
        if (StringUtils.isNotBlank(jsonSupplyAddress)){
            JSONArray jsonArray = JSON.parseArray(jsonSupplyAddress);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                HcSiteAddressDetails details = new HcSiteAddressDetails();
                //不对此地址供餐的商户，因为商户较多，所以只存不对此地址提供供餐的商户id
                String supplyMerchantIds = jsonObject.getString("supplyMerchantIds");
                AtomicReference<String> isShow = new AtomicReference<>("true");
                if (StringUtils.isNotBlank(supplyMerchantIds)){
                    List<String> merchantIds = Arrays.asList(supplyMerchantIds.split(","));
                    merchantIds.forEach(m -> {
                        if (merchantId.equals(m)){
                            isShow.set("false");
                        }
                    });
                }
                details.setIsShowAddress(isShow.get());
                details.setId(addressId);
                details.setSupplyAddressName(jsonObject.getString("name"));
                details.setSupplyAddressCode(jsonObject.getString("code"));
                details.setSupplyAddressImg(jsonObject.getString("img"));
                result.add(details);
            }
            addressDetails.setSupplyAddress(jsonArray.toJSONString());
            this.updateById(addressDetails);

        }
        return result;
    }


    public List<HcSiteAddressDetails> getListByUserNew(String addressId,String merchantIds) {
        HcSiteAddressDetails hcSiteAddressDetails = this.getById(addressId);

        if (hcSiteAddressDetails == null) {
            throw new JeecgBootException("未查询到数据");
        }

        List<HcSiteAddressDetails> result = new ArrayList<>();

        JSONArray jsonArray = JSON.parseArray(hcSiteAddressDetails.getSupplyAddress());
        List<String> merchantIdList = Arrays.asList(merchantIds.split(","));

        for (int i = 0; i < jsonArray.size(); i++) {
            AtomicReference<String> isShow = new AtomicReference<>("true");
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HcSiteAddressDetails details = new HcSiteAddressDetails();
            BeanUtils.copyProperties(hcSiteAddressDetails, details);
            String supplyMerchantIds = jsonObject.getString("supplyMerchantIds");
            if (StringUtils.isNotBlank(supplyMerchantIds)){
                List<String> supplyMerchantIdList = Arrays.asList(supplyMerchantIds.split(","));
                merchantIdList.forEach(s ->{
                    supplyMerchantIdList.forEach(sp ->{
                        if (s.equals(sp)){
                            isShow.set("false");
                            return;
                        }
                    });
                });
            }
            details.setIsShowAddress(isShow.get());
            details.setSupplyAddressName(jsonObject.getString("name"));
            details.setSupplyAddressCode(jsonObject.getString("code"));
            details.setSupplyAddressImg(jsonObject.getString("img"));
            result.add(details);
        }

        return result;
    }
    public List<HcSiteAddressDetails> getListByUser(String addressId) {
        HcSiteAddressDetails hcSiteAddressDetails = this.getById(addressId);

        if (hcSiteAddressDetails == null) {
            throw new JeecgBootException("未查询到数据");
        }

        List<HcSiteAddressDetails> result = new ArrayList<>();

        JSONArray jsonArray = JSON.parseArray(hcSiteAddressDetails.getSupplyAddress());

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HcSiteAddressDetails details = new HcSiteAddressDetails();
            BeanUtils.copyProperties(hcSiteAddressDetails, details);
            details.setSupplyAddressName(jsonObject.getString("name"));
            details.setSupplyAddressCode(jsonObject.getString("code"));
            details.setSupplyAddressImg(jsonObject.getString("img"));
            result.add(details);
        }

        return result;
    }

    public HcSiteAddressDetails getRecentAddress(HcSiteAddressDetails param) {
        return hcSiteAddressDetailsMapper.getRecentAddress(param);
    }

    @Override
    public List<HcSiteAddressDetails> getAllAddress() {
        return hcSiteAddressDetailsMapper.getAllAddress();
    }
}
